#include "StdAfx.h"

AreaDialog* Singleton<AreaDialog>::m_instance = 0;

AreaDialog::AreaDialog()
{
	m_dialog = new Dialog(200, 200, 12, 8, "DBC-Area");
	m_dialog->m_visible = false;
	m_dialog->AddText(30, 30, "Load ID:", 5);
	m_entryBox = m_dialog->AddEditBox(30, 45, 100, 20);
	m_dialog->AddButton(this, 140, 50, "Load", &AreaDialog::LoadEntryProc, 45, 20, 3);
	m_dialog->AddText(30, 80, "Name:");
	m_nameBox = m_dialog->AddEditBox(30, 95, 300, 20);
	m_dialog->AddText(30, 120, "Flags (hex num):");
	m_flagBox = m_dialog->AddEditBox(30, 135, 120, 20);
	m_dialog->AddText(190, 120, "Parent area:");
	m_parentBox = m_dialog->AddEditBox(190, 135, 120, 20);
	m_dialog->AddText(30, 160, "Mapid:");
	m_mapBox = m_dialog->AddEditBox(30, 175, 50, 20);
	m_dialog->AddText(190, 160, "Minlvl:");
	m_levelBox = m_dialog->AddEditBox(190, 175, 50, 20);
	m_dialog->AddText(30, 215, "Save ID:", 6);
	m_saveBox = m_dialog->AddEditBox(100, 212, 100, 20);
	m_dialog->AddButton(this, 210, 50, "Save", &AreaDialog::SaveEntryProc, 212, 20, 4);
	sUI.AddElement(m_dialog);
}

void AreaDialog::LoadEntryProc(UIButton<AreaDialog>*)
{
	std::string szEntry = m_entryBox->GetText();
	ui32 entry = 0;
	if(sscanf(szEntry.c_str(), "%u", &entry) != 1)
	{
		sChat.AddText("Please write an entry to load!", 7);
		return;
	}
	AreaTableEntry* e = dbcArea.LookupEntry(entry);
	if(e == 0)
	{
		sChat.AddText("This entry could not be found!", 8);
		return;
	}
	std::vector<wchar_t> unicode(strlen(e->name) + 1);
	MultiByteToWideChar(CP_UTF8, 0, e->name, unicode.size(), &unicode[0], unicode.size());
	m_nameBox->SetText(&unicode[0]);
	char msg[255];
	sprintf_s<255>(msg, "%08X", e->flags);
	m_flagBox->SetText(msg);
	sprintf_s<255>(msg, "%u", e->rec_id);
	m_parentBox->SetText(msg);
	sprintf_s<255>(msg, "%u", e->map_id);
	m_mapBox->SetText(msg);
	sprintf_s<255>(msg, "%u", e->areaLevel);
	m_levelBox->SetText(msg);
}

void AreaDialog::SaveEntryProc(UIButton<AreaDialog>*)
{
	std::string szEntry = m_saveBox->GetText();
	ui32 entry = 0;
	if(sscanf(szEntry.c_str(), "%u", &entry) != 1)
	{
		sChat.AddText("Please write an entry to save!", 7);
		return;
	}
	AreaTableEntry* e = dbcArea.LookupEntry(entry);
	if(e != 0)
	{
		sChat.AddText("This entry already exists!", 8);
		return;
	}
	ui32 stringPos = dbcArea.AddString(m_nameBox->GetText());
	AreaTableEntry* pEntry = new AreaTableEntry;
	pEntry->areaLevel = atoi(m_levelBox->GetText().c_str());
	pEntry->explore = 0;
	pEntry->flags = atoi(m_flagBox->GetText().c_str());
	pEntry->id = entry;
	pEntry->map_id = atoi(m_mapBox->GetText().c_str());
	pEntry->name = (char*)stringPos;
	memset(pEntry->nneed, 0, 8);
	pEntry->rec_id = atoi(m_parentBox->GetText().c_str());
	pEntry->soundRef = 0;
	pEntry->soundsRef = 0;
	pEntry->unk = 0;
	pEntry->zoneRef = 0;
	pEntry->zoneSec = 0;
	dbcArea.AddRecord(pEntry);
	dbcArea.SaveToFile();
	pEntry = dbcArea.LookupEntry(entry);
	pEntry->name = new char[m_nameBox->GetText().length()];
	strcpy(pEntry->name, m_nameBox->GetText().c_str());
	sChat.AddText("Areaid added!", 9);
}

void AreaDialog::Update()
{
	//m_dialog->m_visible = m_visible;
}